package com.sqfw.project.wpReport.service.impl;

import com.sqfw.common.utils.StringUtils;
import com.sqfw.project.system.domain.SysDictData;
import com.sqfw.project.system.mapper.SysDictDataMapper;
import com.sqfw.project.wp.domain.SysCode;
import com.sqfw.project.wp.domain.WpInfo;
import com.sqfw.project.wp.mapper.SysCodeMapper;
import com.sqfw.project.wp.mapper.WpInfoMapper;
import com.sqfw.project.wp.service.ISysCodeService;
import com.sqfw.project.wpReport.domain.WpInfoSummary;
import com.sqfw.project.wpReport.domain.dto.WpInfoSummaryDTO;
import com.sqfw.project.wpReport.service.IWpInfoSummaryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class WpInfoSummaryServiceImpl implements IWpInfoSummaryService {

    @Autowired
    private WpInfoMapper wpInfoMapper;

    @Autowired
    private SysDictDataMapper dictDataMapper;

    @Autowired
    private SysCodeMapper sysCodeMapper;

    @Autowired
    private ISysCodeService sysCodeService;

    //工单来源 字典类型 scidType
    private static final String scidTypeDictType = "scid_type";
    //工单类型 字典类型  wpType
    private static final String wpTypeDictType = "wp_type";
    //内容分类 pid  与数据库数据对应
    private static final Long sysCodePid = 13552L;


    @Override
    public List<WpInfoSummary> getWpInfoSummaryList(WpInfoSummaryDTO wpInfoSummaryDTO) {

        List<WpInfoSummary> wpInfoSummaryList = new ArrayList<>();
        String selectType = wpInfoSummaryDTO.getSelectType();
        SysDictData dictData = new SysDictData();
        if ("0".equals(selectType)) {
            //工单类型  wpType
            dictData.setDictType(wpTypeDictType);
            List<String> dictValues = dictDataMapper.selectDictDataList(dictData)
                    .stream().map(SysDictData::getDictValue).collect(Collectors.toList());
            wpInfoSummaryDTO.setWpTypes(dictValues);
            List<WpInfo> wpInfos = wpInfoMapper.getWpInfoSummaryList(wpInfoSummaryDTO);
            for (String dictValue : dictValues) {

                List<WpInfo> wpInfoList = wpInfos.stream()
                        .filter(info -> dictValue.equals(info.getWpType()))
                        .collect(Collectors.toList());

                if (StringUtils.isNotEmpty(wpInfoList) && wpInfoList.size() > 0) {
                    WpInfoSummary wpInfoSummary = new WpInfoSummary();
                    wpInfoSummary.setType(dictValue);
                    wpInfoSummary.setWpInfoCount(wpInfoList.size());
                    wpInfoSummaryList.add(wpInfoSummary);
                }
            }
        } else if ("1".equals(selectType)) {
            //内容分类  class1
            SysCode sysCode = new SysCode();
            //n为可用，d为删除
            sysCode.setState("n");
            sysCode.setPid(sysCodePid);
            sysCode.setNodetype("class1");
            List<SysCode> codes = sysCodeMapper.selectSysCodeList(sysCode);
            List<String> class1List = codes.stream()
                    .map(SysCode::getAutoId)
                    .map(String::valueOf)
                    .collect(Collectors.toList());
            wpInfoSummaryDTO.setClass1List(class1List);


            List<WpInfo> wpInfos = wpInfoMapper.getWpInfoSummaryList(wpInfoSummaryDTO);

            for (SysCode code : codes) {
                String codeId = code.getAutoId().toString();
                List<WpInfo> wpInfoList = wpInfos.stream()
                        .filter(info -> codeId.equals(info.getClass1()))
                        .collect(Collectors.toList());

                if (StringUtils.isNotEmpty(wpInfoList) && wpInfoList.size() > 0) {
                    WpInfoSummary wpInfoSummary = new WpInfoSummary();
                    wpInfoSummary.setType(code.getNodename());
                    wpInfoSummary.setWpInfoCount(wpInfoList.size());
                    wpInfoSummary.setCalss1(codeId);
                    wpInfoSummaryList.add(wpInfoSummary);
                }

            }

        } else if ("2".equals(selectType)) {
            //工单来源  scidType
            dictData.setDictType(scidTypeDictType);
            List<String> dictValues = dictDataMapper.selectDictDataList(dictData)
                    .stream().map(SysDictData::getDictValue).collect(Collectors.toList());

            wpInfoSummaryDTO.setScidTypes(dictValues);

            List<WpInfo> wpInfos = wpInfoMapper.getWpInfoSummaryList(wpInfoSummaryDTO);

            for (String dictValue : dictValues) {
                List<WpInfo> wpInfoList = wpInfos.stream()
                        .filter(info -> dictValue.equals(info.getScidType()))
                        .collect(Collectors.toList());
                if (StringUtils.isNotEmpty(wpInfoList) && wpInfoList.size() > 0) {
                    WpInfoSummary wpInfoSummary = new WpInfoSummary();
                    wpInfoSummary.setType(dictValue);
                    wpInfoSummary.setWpInfoCount(wpInfoList.size());

                    wpInfoSummaryList.add(wpInfoSummary);
                }
            }
        }
        return wpInfoSummaryList;
    }

    @Override
    public List<WpInfo> getWpInfoCountList(WpInfo wpInfo){
        String selectType = wpInfo.getSelectType();
        if ("0".equals(selectType)) {

            wpInfo.setWpType(wpInfo.getType());
            return getWpInfoClass(wpInfoMapper.selectWpInfoList(wpInfo));

        } else if ("1".equals(selectType)) {
            if ("民政业务".equals(wpInfo.getType())){
                wpInfo.setClass1("13553");
            }else if ("政务导航".equals(wpInfo.getType())){

                wpInfo.setClass1("13554");
            }else if ("其他".equals(wpInfo.getType())){
                wpInfo.setClass1("14478");

            }

            return getWpInfoClass(wpInfoMapper.selectWpInfoList(wpInfo));
        } else if ("2".equals(selectType)) {
            wpInfo.setScidType(wpInfo.getType());
            log.info("查询类型ScidType：{}",wpInfo.getType());
            long l = System.currentTimeMillis();
            List<WpInfo> wpInfos = wpInfoMapper.selectWpInfoList(wpInfo);
            log.info("查询类型执行sql时间：{}",System.currentTimeMillis()-l);
            return getWpInfoClass(wpInfos);
        }

        return new ArrayList<WpInfo>();
    }

    public List<WpInfo> getWpInfoClass(List<WpInfo> wpInfos){

        //class1，class2，class3，class4
        List<SysCode> class1Codes = sysCodeService.selectSysCodeClass1List("class1", null);

        List<Long> class2pids = class1Codes.stream().map(SysCode::getAutoId).collect(Collectors.toList());
        List<SysCode> class2Codes = sysCodeService.selectSysCodeClass1List("class2", class2pids);

        List<Long> class3pids = class2Codes.stream().map(SysCode::getAutoId).collect(Collectors.toList());
        List<SysCode> class3Codes = sysCodeService.selectSysCodeClass1List("class3", class3pids);


        List<Long> class4pids = class3Codes.stream().map(SysCode::getAutoId).collect(Collectors.toList());
        List<SysCode> class4Codes = sysCodeService.selectSysCodeClass1List("class4", class4pids);
        for (WpInfo info : wpInfos) {
            String class1 = info.getClass1();
            String class2 = info.getClass2();
            String class3 = info.getClass3();
            String class4 = info.getClass4();

            List<String> nameList = new ArrayList<>();

            if (StringUtils.isNotEmpty(class1)) {
                class1Codes.stream().filter(sysCode -> sysCode.getAutoId().toString().equals(class1))
                        .findAny().ifPresent(code -> nameList.add(code.getNodename()));

            }

            if (StringUtils.isNotEmpty(class2)) {
                class2Codes.stream().filter(sysCode -> sysCode.getAutoId().toString().equals(class2))
                        .findAny().ifPresent(code -> nameList.add(code.getNodename()));
            }
            if (StringUtils.isNotEmpty(class3)) {
                class3Codes.stream().filter(sysCode -> sysCode.getAutoId().toString().equals(class3))
                        .findAny().ifPresent(code -> nameList.add(code.getNodename()));

            }
            if (StringUtils.isNotEmpty(class4)) {

                class4Codes.stream().filter(sysCode -> sysCode.getAutoId().toString().equals(class4))
                        .findAny().ifPresent(code -> nameList.add(code.getNodename()));

            }

            String collect = String.join("/", nameList);
            /*if (StringUtils.isNotEmpty(class1)) {
                SysCode sysCode = sysCodeMapper.selectSysCodeByAutoId(Long.valueOf(class1));
                if (sysCode!=null){
                    info.setMainNodename(sysCode.getNodename());
                }

            }

            if (StringUtils.isNotEmpty(class2)) {
                SysCode sysCode = sysCodeMapper.selectSysCodeByAutoId(Long.valueOf(class2));
                if (sysCode!=null){
                    name.append(sysCode.getNodename());
                }
            }
            if (StringUtils.isNotEmpty(class3)) {
                SysCode sysCode = sysCodeMapper.selectSysCodeByAutoId(Long.valueOf(class3));
                if (sysCode!=null){
                    name.append("/").append(sysCode.getNodename());
                }

            }
            if (StringUtils.isNotEmpty(class4)) {

                SysCode sysCode = sysCodeMapper.selectSysCodeByAutoId(Long.valueOf(class4));
                if (sysCode!=null){
                    name.append("/").append(sysCode.getNodename());
                }

            }*/
            info.setChildNodeName(collect);
            /*if (StringUtils.isNotEmpty(name)) {

            }*/
        }
       return wpInfos;
    }
}
